package {
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.BulkProgressEvent;
	
	import com.debug.Debug;
	import com.debug.TDebug;
	import com.menu.MyContextMenu;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLStream;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	import com.manager.SkinManager;
	import manager.xml.XMLConfig;
	
	import org.adobe.basecode.Base64;
	import org.adobe.basecode.PNGDecoder;
	import org.adobe.basecode.PNGEncoder;
	
	[SWF(width=550,height=450)]
	public class Stream extends Sprite {
		private var _loader:Loader;
		private var stream:URLStream;
		private var byte:ByteArray;
		
		public function Stream() {
			Debug.trace("aaa","bbb","ccc");
			byte = new ByteArray;
			/**urlLoader加载图片*/
			var urlloader:URLLoader = new URLLoader();
			/*urlloader.dataFormat = URLLoaderDataFormat.BINARY; 
			urlloader.addEventListener(Event.COMPLETE,urlHandler);
			urlloader.load(new URLRequest("test.png"));*/
			
			/**Loader*/
			var loader:Loader = new Loader();
			//loader.contentLoaderInfo.addEventListener(Event.COMPLETE,handler);
			//loader.load(new URLRequest("pic.jpg"));
			
			/**urlStream*/
			stream = new URLStream();
			stream.addEventListener(ProgressEvent.PROGRESS,progress);
			//stream.addEventListener(Event.COMPLETE,streamComplete);
			stream.load(new URLRequest("http://www.m1905.com/uploadfile/2009/0910/468/20090910043225782.jpg"));
			_loader = new Loader();
			this.addChild(_loader);
		}
		
		private function progress(e:ProgressEvent):void{
			var count:int = 50000;
			if (stream.connected){
				stream.readBytes(byte, byte.length);
			}
			if(e.bytesLoaded > count){
				//stream.close();
			}
			trace(byte.length);
			showImage();
		}
		public function showImage():void {
			if (stream.connected){
				stream.readBytes(byte, byte.length);
			}
			_loader.unload();
			_loader.loadBytes(byte);
		}
		
		public function streamComplete(e:Event):void {  
			stream.removeEventListener(Event.COMPLETE, streamComplete);  
			var byte:ByteArray = new ByteArray();  
			stream.readBytes(byte);  //从stream写入byte中
			
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderHandler);  
			_loader.loadBytes(byte);  
			if(stream.connected){
				stream.close();  
			}
			
		}  
		private function onLoaderHandler(e:Event):void{  
			e.target.removeEventListener(Event.COMPLETE,onLoaderHandler);  
		} 

		/**urlloader加载,加载的数据写入byte中*/
		private function urlHandler(e:Event):void{
			//var byte:ByteArray = e.target.data as ByteArray;
			var byte:ByteArray = new ByteArray;
			byte.writeBytes(e.target.data);
			byte.position = 0;
			
			var load:Loader=new Loader();    
			load.loadBytes(byte);//读取ByteArray    
			load.contentLoaderInfo.addEventListener(Event.COMPLETE, compl); 
		}
		
		/**loader加载完*/
		private function handler(e:Event):void{
			trace(e.target,e.target.content);
			var bit:Bitmap = e.target.content as Bitmap;
			var byte:ByteArray = PNGEncoder.encode(bit.bitmapData);
			var str:String = Base64.encodeByteArray(byte);//使用base64转为字
			
			var byte1:ByteArray = Base64.decodeToByteArray(str);
			var load:Loader=new Loader();    
			load.loadBytes(byte1);//读取ByteArray    
			load.contentLoaderInfo.addEventListener(Event.COMPLETE, compl);    
		}
		private function compl(event:Event):void {    
			var bitMap:Bitmap=event.target.content as Bitmap;//读取Bitmap    
			trace(bitMap.width,bitMap.height,bitMap.alpha);
			this.addChild(bitMap);    
		}  
		
		
		
		
		private function loaderSWF():void{
			var request:URLRequest = new URLRequest("assert/library.swf");
			var loader:Loader = new Loader();
			var context:LoaderContext = new LoaderContext(false,ApplicationDomain.currentDomain);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderHandler);
			loader.load(request,context);
		}
		private function loaderHandler(e:Event):void{
			var tag:LoaderInfo = e.target as LoaderInfo;
			var cls:Class;
			if(tag.applicationDomain.hasDefinition("cls")){
				cls = tag.applicationDomain.getDefinition("cls") as Class;
			}
			if(cls==null && ApplicationDomain.currentDomain.hasDefinition("cls")){
				cls = ApplicationDomain.currentDomain.getDefinition("cls") as Class;
			}
			if(cls!=null){
				this.addChild(new cls());
			}
		}


		
		private function complete(e:Event):void{
			//trace(XML(e.target.data));
		}
		private function setDictionary():void{
			var dict:Dictionary = new Dictionary();
			for(var i:uint; i<10; i++){
				dict[i] = "dict" + i;
			}
			for(var key:* in dict){
				trace(key);
			}
			for each(var val:* in dict){
				trace(val);
			}
		}
	}
}